package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-03
 * Time: 17:44
 */
class Singleton_Lazy{
    //volatile取消指令重排序
    private static volatile Singleton_Lazy lazy = null;

    private static final Object object = new Object();

    public static Singleton_Lazy get(){
        //优化,提高性能
        if (lazy == null) {
            //避免多线程时实例化多个对象
            synchronized (object) {
                if (lazy == null) {
                    lazy = new Singleton_Lazy();
                }
            }
        }
        return lazy;
    }

    private Singleton_Lazy(){}
}

public class demo2 {
    public static void main(String[] args) {
        Singleton_Lazy object1 = Singleton_Lazy.get();
        Singleton_Lazy object2 = Singleton_Lazy.get();
        System.out.println(object1);
        System.out.println(object2);
        System.out.println(object1 == object2);
    }
}
